#pragma once 

namespace my_compare
{
    template<class T>
    struct less
    {
        bool operator()(const T& l, const T& r)
        {
            return l < r;
        }
    };

    template<class T>
    class greater
    {
        bool operator()(const T& l, const T& r)
        {
            return l > r;
        }
    };
}

namespace my_pri_queue
{
    template<class T, class Container = vector<T>, class Compare = my_compare::less<T>>
    class priority_queue 
    {
    public:
        
        priority_queue()
        {}

        //Container::iterator begin()
        //{
        //    return _con.begin();
        //}
        //Container::const_iterator begin() const 
        //{
        //    return _con.begin();
        //}

        //Container::iterator end() 
        //{
        //    return _con.end();
        //}
        //Container::const_iterator end() const 
        //{
        //    return _con.end();
        //}

        template<class InputIterator>
        priority_queue(InputIterator first, InputIterator last)
        {
            while (first != last)
            {
                _con.push_back(*first);
                ++ first;
            }

            for (int i = (_con.size() - 1 - 1) / 2; i >= 0; i -- )
            {
                adjustdown(i);
            }
        }

        priority_queue(const priority_queue& q)
        {
            _con = q._con;
        }

        priority_queue& operator=(const priority_queue& q)
        {
            _con = q._con;

            return *this;
        }

        void adjustdown(size_t parent)
        {
            Compare com;

            size_t child = parent * 2 + 1;
            while (child < size())
            {
                if (child + 1 < size() && com(_con[child], _con[child + 1]))
                {
                    child ++ ;
                }

                if (com(_con[parent], _con[child]))
                {
                    swap(_con[child], _con[parent]);
                    parent = child;
                    child = parent * 2 + 1;
                }
                else 
                {
                    break;
                }
            }
        }

        void adjustup(size_t child)
        {
            Compare com;

            size_t parent = (child - 1) / 2;
            while (child > 0)
            {
                if (com(_con[parent], _con[child]))
                {
                    swap(_con[child], _con[parent]);
                    child = parent;
                    parent = (child - 1) / 2;
                }
                else 
                {
                    break;
                }
            }
        }

        void push(const T& x)
        {
            _con.push_back(x); 
            adjustup(_con.size() - 1);
        }

        void pop()
        {
            std::swap(_con[0], _con[_con.size() - 1]);
            _con.pop_back();

            adjustdown(0);
        }

        const T& top() const 
        {
            return _con[0];
        }

        bool empty() const 
        {
            return _con.empty();
        }

        size_t size() const 
        {
            return _con.size();
        }

    private:
        
        Container _con;
    };
}
